package com.dstz.crm.groovy;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.dstz.base.api.exception.BusinessException;
import com.dstz.base.api.query.QueryFilter;
import com.dstz.base.api.query.QueryOP;
import com.dstz.base.core.util.BeanUtils;
import com.dstz.base.db.model.query.DefaultQueryFilter;
import com.dstz.crm.core.dao.CustomerApplicationDao;
import com.dstz.crm.core.dao.CustomerDao;
import com.dstz.crm.core.dao.CustomerRespondentDao;
import com.dstz.crm.core.model.CustomerApplication;
import com.dstz.crm.core.model.CustomerRespondent;
import com.dstz.org.api.model.IGroup;
import com.dstz.org.api.model.IUser;
import com.dstz.sys.api.groovy.IScript;
import com.dstz.sys.api.service.SerialNoService;
import com.dstz.sys.util.ContextUtil;

/**
 * 系统脚本
 * 常用的系统功能的脚本
 */
@Component
public class CrmScript implements IScript {
	@Resource
	CustomerApplicationDao customerApplicationDao;
	@Resource
	CustomerRespondentDao customerRespondentDao;
	@Resource
	CustomerDao customerDao;
	
	public void applyAgree(String id) {
		CustomerApplication customerApplication = customerApplicationDao.get(id);
		if(customerApplication==null) {
			throw new BusinessException("无此申请记录");
		}
		String applicantId = customerApplication.getApplicantId();
		QueryFilter queryFilter=new DefaultQueryFilter();
		queryFilter.setPage(null);
		queryFilter.addFilter("application_id_", id, QueryOP.EQUAL);
		List<CustomerRespondent> query = customerRespondentDao.query(queryFilter);
		if(query==null||query.size()<=0) {
			throw new BusinessException("此申请记录客户列表为空");
		}
		List<String> customers=new ArrayList<>();
		for (CustomerRespondent customerRespondent : query) {
			customers.add(customerRespondent.getCustomerId());
		}
		customerDao.updateCustomerTypeBatch(applicantId,customers);
		
	}
}
